package org.gongliang.jsmile.authc.task.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.gongliang.jsmile.authc.task.entity.QrtzTask;
import org.gongliang.jsmile.authc.task.service.QrtzTaskService;
import org.gongliang.jsmile.common.base.BaseController;
import org.gongliang.jsmile.common.base.JSmileResult;
import org.gongliang.jsmile.common.enums.KException;
import org.gongliang.jsmile.common.kit.web.ShiroKit;
import org.gongliang.jsmile.common.kit.web.WebKit;
import org.gongliang.jsmile.config.log.info.KLog;
import org.gongliang.jsmile.config.log.info.LogType;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;

import io.swagger.annotations.ApiOperation;

/**
* @desc   控制器
* @author 龚亮
* @version 1.0
* @date 2018年08月25日 05:30:27
*/
@Controller
@RequestMapping("/qrtz_task")
public class QrtzTaskController extends BaseController {

    @Autowired
    private QrtzTaskService qrtzTaskService;
    
	@GetMapping("/main")
	public String main() {
		return "sys/Task/task";
	}
	@GetMapping("/add")
	public String add() {
		return "sys/Task/add";
	}

	@GetMapping("/edit")
	public String edit() {
		return "sys/Task/edit";
	}
    
	/**
	 * 查询所有分页 返回 Lit<Map>
	 */
	@ResponseBody
	@ApiOperation(value = "任务列表")
	@RequestMapping("/findAllByPage")
	@KLog(title = "任务管理->分页获取任务列表", type = LogType.QUERY)
	public JSmileResult findAllMapByPage(HttpServletRequest request) {
		Map<String, Object> reqMap = WebKit.getParams(request);
		WebKit.setPage(WebKit.toPageIndex(reqMap), WebKit.toPageSize(reqMap));
		List<QrtzTask> list = qrtzTaskService.findAll(reqMap);
		PageInfo<QrtzTask> pages = new PageInfo<QrtzTask>(list);
		JSmileResult result = JSmileResult.success();
		result.setResData(pages.getList(), pages.getTotal());
		return result;
	}
	
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("sys:task:add")
	@KLog(title = "任务管理->添加一个定时任务", type = LogType.INSERT)
	public JSmileResult save(QrtzTask task,HttpServletRequest request) throws SchedulerException {
		JSmileResult result=JSmileResult.success("添加定时任务成功!");
		task.setCreateDate(new Date());
		task.setCreator(ShiroKit.getUserId());
		if(!qrtzTaskService.saveTask(task)) {
			result.setResState(JSmileResult.STATE_ERROR);
			result.setResMsg("添加定时任务失败!");
		}
		return  result;
	}
	@ResponseBody
	@PostMapping("/update")
	@RequiresPermissions("sys:task:update")
	@KLog(title = "任务管理->修改定时任务", type = LogType.UPDATE)
	public JSmileResult update(QrtzTask task,HttpServletRequest request) {
		JSmileResult result=JSmileResult.success("修改定时任务成功!");
		task.setUpdater(ShiroKit.getUserId());
		task.setUpdateDate(new Date());
		if(!qrtzTaskService.updateById(task)) {
			result.setResState(JSmileResult.STATE_ERROR);
			result.setResMsg("修改定时任务失败!");
		}
		return  result;
	}
	@ResponseBody
	@PostMapping("/pauseJob")
	@KLog(title = "任务管理->暂停任务", type = LogType.UPDATE)
	public JSmileResult pauseJob(QrtzTask task,HttpServletRequest request) throws SchedulerException {
		JSmileResult result=JSmileResult.success("暂停定时任务成功!");
		task.setUpdater(ShiroKit.getUserId());
		task.setUpdateDate(new Date());
		if(!qrtzTaskService.pauseJob(task)) {
			result.setResState(JSmileResult.STATE_ERROR);
			result.setResMsg("暂停任务失败!");
		}
		return  result;
	}
	@ResponseBody
	@PostMapping("/resumeJob")
	@KLog(title = "任务管理->恢复任务", type = LogType.UPDATE)
	public JSmileResult resumeJob(QrtzTask task,HttpServletRequest request) throws SchedulerException{
		JSmileResult result=JSmileResult.success("恢复任务成功!");
		task.setUpdater(ShiroKit.getUserId());
		task.setUpdateDate(new Date());
		if(!qrtzTaskService.resumeJob(task)) {
			result.setResState(JSmileResult.STATE_ERROR);
			result.setResMsg("恢复任务失败!");
		}
		return  result;
	}
	@ResponseBody
	@PostMapping("/runJob")
	@KLog(title = "任务管理->立即运行一次定时任务", type = LogType.UPDATE)
	public JSmileResult runJob(QrtzTask task,HttpServletRequest request) throws SchedulerException{
		JSmileResult result=JSmileResult.success("立即运行任务成功!");
		task.setUpdater(ShiroKit.getUserId());
		task.setUpdateDate(new Date());
		if(!qrtzTaskService.runJob(task)) {
			result.setResState(JSmileResult.STATE_ERROR);
			result.setResMsg("立即运行任务失败!");
		}
		return  result;
	}


	/**
	 * 根据一组id删除
	 * @throws SchedulerException 
	 */
	@ResponseBody
	@PostMapping("/deleteByIds")
	@RequiresPermissions("sys:task:delete")
	@KLog(title = "任务管理->删除任务信息", type = LogType.DELETE)
	public JSmileResult deleteByIds(@RequestParam(value = "ids[]", required = true) String[] ids) throws SchedulerException {
		JSmileResult result=JSmileResult.success();
		if (ids == null || ids.length == 0) {
			return JSmileResult.error(KException.IDS_NOT_NULL);
		}
		if(!qrtzTaskService.deleteTasks(ids)) {
			result.setResState(JSmileResult.STATE_ERROR);
			result.setResMsg("删除定时任务失败!");
		}
		return  result;
	}
}
